package com.hfl.core.des;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;

/**
 * Created by Administrator on 2016-9-10.
 */

    public class DESUtils {
        private static Key key;
        private static String KEY_STR = "rj_hfl";
        static {
            try {
                KeyGenerator generator = KeyGenerator.getInstance("DES");
                generator.init(new SecureRandom(KEY_STR.getBytes()));
                key = generator.generateKey();
                generator = null;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        /**
         * 对str进行DES加密
         *
         * @param str
         * @return
         */
        public static String getEncryptString(String str) {
            BASE64Encoder base64en = new BASE64Encoder();
            try {
                byte[] strBytes = str.getBytes("UTF8");
                Cipher cipher = Cipher.getInstance("DES");
                cipher.init(Cipher.ENCRYPT_MODE, key);
                byte[] encryptStrBytes = cipher.doFinal(strBytes);
                return base64en.encode(encryptStrBytes);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        /**
         * 对str进行DES解密
         *
         * @param str
         * @return
         */
        public static String getDecryptString(String str) {
            BASE64Decoder base64De = new BASE64Decoder();
            try {
                byte[] strBytes = base64De.decodeBuffer(str);
                Cipher cipher = Cipher.getInstance("DES");
                cipher.init(Cipher.DECRYPT_MODE, key);
                byte[] decryptStrBytes = cipher.doFinal(strBytes);
                return new String(decryptStrBytes, "UTF8");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }

        public static void main(String[] args) throws Exception {
//		if (args == null || args.length < 1) {
//			System.out.println("请输入要加密的字符，用空格分�?");
//		} else {
//			for (String arg : args) {
//				System.out.println(arg + ":" + getEncryptString(arg));
//			}
//		}

            try {
                System.out.println(getEncryptString("root"));
                System.out.println(getEncryptString("123456"));
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

        }
}
